<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <title>Film Grain Post-processing — Alien.js</title>

    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto+Mono">
    <link rel="stylesheet" href="../assets/css/style.css">

    <script type="module">
        import { BloomCompositeMaterial, BoxGeometry, BufferGeometry, Color, ColorManagement, DirectionalLight, Float32BufferAttribute, GLSL3, Group, LinearSRGBColorSpace, LuminosityMaterial, MathUtils, Mesh, MeshStandardMaterial, NoBlending, OrthographicCamera, PanelItem, PlaneGeometry, RawShaderMaterial, Scene, UI, UnrealBloomBlurMaterial, Vector2, WebGLRenderTarget, WebGLRenderer, getFullscreenTriangle, getKeyByValue, ticker } from '../../build/alien.three.js';

        const breakpoint = 1000;

        import random from '../../src/shaders/modules/random/random.glsl.js';
        import rgbshift from '../../src/shaders/modules/rgbshift/rgbshift.glsl.js';

        class CompositeMaterial extends RawShaderMaterial {
            constructor() {
                const { time } = WorldController;

                super({
                    glslVersion: GLSL3,
                    uniforms: {
                        tScene: { value: null },
                        tBloom: { value: null },
                        uBloomDistortion: { value: 1.5 },
                        uGrainAmount: { value: 0.2 },
                        uReduction: { value: 0.9 },
                        uBoost: { value: 1.1 },
                        uTime: time
                    },
                    vertexShader: /* glsl */ `
                        in vec3 position;
                        in vec2 uv;

                        out vec2 vUv;

                        void main() {
                            vUv = uv;

                            gl_Position = vec4(position, 1.0);
                        }
                    `,
                    fragmentShader: /* glsl */ `
                        precision highp float;

                        uniform sampler2D tScene;
                        uniform sampler2D tBloom;
                        uniform float uBloomDistortion;
                        uniform float uGrainAmount;
                        uniform float uReduction;
                        uniform float uBoost;
                        uniform float uTime;

                        in vec2 vUv;

                        out vec4 FragColor;

                        ${random}
                        ${rgbshift}

                        void main() {
                            float center = length(vUv - 0.5);

                            FragColor = texture(tScene, vUv);

                            float angle = length(vUv - 0.5);
                            float amount = 0.001 * uBloomDistortion;

                            FragColor.rgb += getRGB(tBloom, vUv, angle, amount).rgb;

                            // Film grain
                            FragColor.rgb += vec3(uGrainAmount * random(vUv + vec2(uTime, 0.0) * 0.06));

                            // Vignetting
                            FragColor.rgb *= uBoost - center * uReduction;
                        }
                    `,
                    blending: NoBlending,
                    depthTest: false,
                    depthWrite: false
                });
            }
        }

        // Based on https://codepen.io/zachgoldstein/pen/LXRXVw

        class SceneView extends Group {
            constructor() {
                super();

                this.initMesh();
            }

            initMesh() {
                const material = new MeshStandardMaterial({ metalness: 1 });

                // Penrose triangle geometry
                const bottomBox = new BoxGeometry(50, 50, 300);
                const bottomMesh = new Mesh(bottomBox, material);
                this.add(bottomMesh);

                const upBox = new BoxGeometry(50, 250, 50);
                const upMesh = new Mesh(upBox, material);
                upMesh.position.set(0, 150, 125);
                this.add(upMesh);

                const topBox = new BoxGeometry(150, 50, 50);
                const topMesh = new Mesh(topBox, material);
                topMesh.position.set(100, 250, 125);
                this.add(topMesh);

                const topPlane = new PlaneGeometry(50, 50);
                const topPlaneMesh = new Mesh(topPlane, material);
                topPlaneMesh.position.set(200, 275, 125);
                topPlaneMesh.rotation.x = MathUtils.degToRad(-90);
                this.add(topPlaneMesh);

                const topTri = new BufferGeometry();
                topTri.setAttribute('position', new Float32BufferAttribute([0, 0, 0, 50, 0, 0, 0, 50, 0], 3));
                topTri.setAttribute('uv', new Float32BufferAttribute([0, 0, 1, 0, 0, 1], 2));
                topTri.computeVertexNormals();
                const topTriMesh = new Mesh(topTri, material);
                topTriMesh.position.set(175, 275, 150);
                topTriMesh.scale.y = -1;
                this.add(topTriMesh);

                // Centre
                this.position.y = -62.5;
            }
        }

        class PanelController {
            static init(ui) {
                this.ui = ui;

                this.initPanel();
            }

            static initPanel() {
                const { luminosityMaterial, bloomCompositeMaterial, compositeMaterial } = RenderManager;

                const postOptions = {
                    Off: false,
                    Post: true
                };

                const items = [
                    {
                        name: 'FPS'
                    },
                    {
                        type: 'divider'
                    },
                    {
                        type: 'list',
                        list: postOptions,
                        value: getKeyByValue(postOptions, RenderManager.enabled),
                        callback: value => {
                            RenderManager.enabled = postOptions[value];
                        }
                    },
                    {
                        type: 'divider'
                    },
                    {
                        type: 'slider',
                        name: 'Grain',
                        min: 0,
                        max: 1,
                        step: 0.01,
                        value: compositeMaterial.uniforms.uGrainAmount.value,
                        callback: value => {
                            compositeMaterial.uniforms.uGrainAmount.value = value;
                        }
                    },
                    {
                        type: 'slider',
                        name: 'Reduce',
                        min: 0,
                        max: 2,
                        step: 0.01,
                        value: compositeMaterial.uniforms.uReduction.value,
                        callback: value => {
                            compositeMaterial.uniforms.uReduction.value = value;
                        }
                    },
                    {
                        type: 'slider',
                        name: 'Boost',
                        min: 0,
                        max: 2,
                        step: 0.01,
                        value: compositeMaterial.uniforms.uBoost.value,
                        callback: value => {
                            compositeMaterial.uniforms.uBoost.value = value;
                        }
                    },
                    {
                        type: 'divider'
                    },
                    {
                        type: 'slider',
                        name: 'Thresh',
                        min: 0,
                        max: 1,
                        step: 0.01,
                        value: luminosityMaterial.uniforms.uThreshold.value,
                        callback: value => {
                            luminosityMaterial.uniforms.uThreshold.value = value;
                        }
                    },
                    {
                        type: 'slider',
                        name: 'Smooth',
                        min: 0,
                        max: 1,
                        step: 0.01,
                        value: luminosityMaterial.uniforms.uSmoothing.value,
                        callback: value => {
                            luminosityMaterial.uniforms.uSmoothing.value = value;
                        }
                    },
                    {
                        type: 'slider',
                        name: 'Strength',
                        min: 0,
                        max: 2,
                        step: 0.01,
                        value: RenderManager.bloomStrength,
                        callback: value => {
                            RenderManager.bloomStrength = value;
                            bloomCompositeMaterial.uniforms.uBloomFactors.value = RenderManager.bloomFactors();
                        }
                    },
                    {
                        type: 'slider',
                        name: 'Radius',
                        min: 0,
                        max: 1,
                        step: 0.01,
                        value: RenderManager.bloomRadius,
                        callback: value => {
                            RenderManager.bloomRadius = value;
                            bloomCompositeMaterial.uniforms.uBloomFactors.value = RenderManager.bloomFactors();
                        }
                    },
                    {
                        type: 'slider',
                        name: 'Chroma',
                        min: 0,
                        max: 10,
                        step: 0.1,
                        value: compositeMaterial.uniforms.uBloomDistortion.value,
                        callback: value => {
                            compositeMaterial.uniforms.uBloomDistortion.value = value;
                        }
                    }
                ];

                items.forEach(data => {
                    this.ui.addPanel(new PanelItem(data));
                });
            }
        }

        const BlurDirectionX = new Vector2(1, 0);
        const BlurDirectionY = new Vector2(0, 1);

        class RenderManager {
            static init(renderer, scene, camera) {
                this.renderer = renderer;
                this.scene = scene;
                this.camera = camera;

                // Film grain and vignetting
                this.grainAmount = 0.2;
                this.reduction = 0.9;
                this.boost = 1.1;

                // Unreal bloom
                this.luminosityThreshold = 0.1;
                this.luminositySmoothing = 1;
                this.bloomStrength = 0.3;
                this.bloomRadius = 0.2;
                this.bloomDistortion = 1;

                this.enabled = true;

                this.initRenderer();
            }

            static initRenderer() {
                const { screenTriangle } = WorldController;

                // Fullscreen triangle
                this.screenCamera = new OrthographicCamera(-1, 1, 1, -1, 0, 1);
                this.screen = new Mesh(screenTriangle);
                this.screen.frustumCulled = false;

                // Render targets
                this.renderTarget = new WebGLRenderTarget(1, 1, {
                    depthBuffer: false
                });

                this.renderTargetBright = this.renderTarget.clone();
                this.renderTargetsHorizontal = [];
                this.renderTargetsVertical = [];
                this.nMips = 5;

                for (let i = 0, l = this.nMips; i < l; i++) {
                    this.renderTargetsHorizontal.push(this.renderTarget.clone());
                    this.renderTargetsVertical.push(this.renderTarget.clone());
                }

                this.renderTarget.depthBuffer = true;

                // Luminosity high pass material
                this.luminosityMaterial = new LuminosityMaterial();
                this.luminosityMaterial.uniforms.uThreshold.value = this.luminosityThreshold;
                this.luminosityMaterial.uniforms.uSmoothing.value = this.luminositySmoothing;

                // Separable Gaussian blur materials
                this.blurMaterials = [];

                const kernelSizeArray = [3, 5, 7, 9, 11];

                for (let i = 0, l = this.nMips; i < l; i++) {
                    this.blurMaterials.push(new UnrealBloomBlurMaterial(kernelSizeArray[i]));
                }

                // Unreal bloom composite material
                this.bloomCompositeMaterial = new BloomCompositeMaterial({ dithering: true });
                this.bloomCompositeMaterial.uniforms.tBlur1.value = this.renderTargetsVertical[0].texture;
                this.bloomCompositeMaterial.uniforms.tBlur2.value = this.renderTargetsVertical[1].texture;
                this.bloomCompositeMaterial.uniforms.tBlur3.value = this.renderTargetsVertical[2].texture;
                this.bloomCompositeMaterial.uniforms.tBlur4.value = this.renderTargetsVertical[3].texture;
                this.bloomCompositeMaterial.uniforms.tBlur5.value = this.renderTargetsVertical[4].texture;
                this.bloomCompositeMaterial.uniforms.uBloomFactors.value = this.bloomFactors();

                // Composite material
                this.compositeMaterial = new CompositeMaterial();
                this.compositeMaterial.uniforms.uBloomDistortion.value = this.bloomDistortion;
                this.compositeMaterial.uniforms.uGrainAmount.value = this.grainAmount;
                this.compositeMaterial.uniforms.uReduction.value = this.reduction;
                this.compositeMaterial.uniforms.uBoost.value = this.boost;
            }

            static bloomFactors() {
                const bloomFactors = [1, 0.8, 0.6, 0.4, 0.2];

                for (let i = 0, l = this.nMips; i < l; i++) {
                    const factor = bloomFactors[i];
                    bloomFactors[i] = this.bloomStrength * MathUtils.lerp(factor, 1.2 - factor, this.bloomRadius);
                }

                return bloomFactors;
            }

            // Public methods

            static resize = (width, height, dpr) => {
                this.renderer.setPixelRatio(dpr);
                this.renderer.setSize(width, height);

                width = Math.round(width * dpr);
                height = Math.round(height * dpr);

                this.renderTarget.setSize(width, height);

                // Unreal bloom
                width = Math.round(width / 2);
                height = Math.round(height / 2);

                this.renderTargetBright.setSize(width, height);

                for (let i = 0, l = this.nMips; i < l; i++) {
                    this.renderTargetsHorizontal[i].setSize(width, height);
                    this.renderTargetsVertical[i].setSize(width, height);

                    this.blurMaterials[i].uniforms.uResolution.value.set(width, height);

                    width = Math.round(width / 2);
                    height = Math.round(height / 2);
                }
            };

            static update = () => {
                const renderer = this.renderer;
                const scene = this.scene;
                const camera = this.camera;

                if (!this.enabled) {
                    renderer.setRenderTarget(null);
                    renderer.render(scene, camera);
                    return;
                }

                const renderTarget = this.renderTarget;
                const renderTargetBright = this.renderTargetBright;
                const renderTargetsHorizontal = this.renderTargetsHorizontal;
                const renderTargetsVertical = this.renderTargetsVertical;

                // Scene pass
                renderer.setRenderTarget(renderTarget);
                renderer.render(scene, camera);

                // Extract bright areas
                this.luminosityMaterial.uniforms.tMap.value = renderTarget.texture;
                this.screen.material = this.luminosityMaterial;
                renderer.setRenderTarget(renderTargetBright);
                renderer.render(this.screen, this.screenCamera);

                // Blur all the mips progressively
                let inputRenderTarget = renderTargetBright;

                for (let i = 0, l = this.nMips; i < l; i++) {
                    this.screen.material = this.blurMaterials[i];

                    this.blurMaterials[i].uniforms.tMap.value = inputRenderTarget.texture;
                    this.blurMaterials[i].uniforms.uDirection.value = BlurDirectionX;
                    renderer.setRenderTarget(renderTargetsHorizontal[i]);
                    renderer.render(this.screen, this.screenCamera);

                    this.blurMaterials[i].uniforms.tMap.value = this.renderTargetsHorizontal[i].texture;
                    this.blurMaterials[i].uniforms.uDirection.value = BlurDirectionY;
                    renderer.setRenderTarget(renderTargetsVertical[i]);
                    renderer.render(this.screen, this.screenCamera);

                    inputRenderTarget = renderTargetsVertical[i];
                }

                // Composite all the mips
                this.screen.material = this.bloomCompositeMaterial;
                renderer.setRenderTarget(renderTargetsHorizontal[0]);
                renderer.render(this.screen, this.screenCamera);

                // Composite pass (render to screen)
                this.compositeMaterial.uniforms.tScene.value = renderTarget.texture;
                this.compositeMaterial.uniforms.tBloom.value = renderTargetsHorizontal[0].texture;
                this.screen.material = this.compositeMaterial;
                renderer.setRenderTarget(null);
                renderer.render(this.screen, this.screenCamera);
            };
        }

        class WorldController {
            static init() {
                this.initWorld();
                this.initLights();

                this.addListeners();
            }

            static initWorld() {
                this.renderer = new WebGLRenderer({
                    powerPreference: 'high-performance',
                    antialias: true
                });

                // Output canvas
                this.element = this.renderer.domElement;

                // Disable color management
                ColorManagement.enabled = false;
                this.renderer.outputColorSpace = LinearSRGBColorSpace;

                // Isometric scene
                this.scene = new Scene();
                this.scene.background = new Color(0x060606);
                this.camera = new OrthographicCamera();
                this.camera.near = 0;
                this.camera.far = 3000;
                this.camera.zoom = 1;
                this.camera.position.set(500, 500, 500);
                this.camera.lookAt(this.scene.position);

                // Global geometries
                this.screenTriangle = getFullscreenTriangle();

                // Global uniforms
                this.resolution = { value: new Vector2() };
                this.texelSize = { value: new Vector2() };
                this.aspect = { value: 1 };
                this.time = { value: 0 };
                this.frame = { value: 0 };
            }

            static initLights() {
                const light = new DirectionalLight(0xffffff, 7);
                light.position.set(60, 100, 20);
                this.scene.add(light);
            }

            static addListeners() {
                this.renderer.domElement.addEventListener('touchstart', this.onTouchStart);
            }

            // Event handlers

            static onTouchStart = e => {
                e.preventDefault();
            };

            // Public methods

            static resize = (width, height, dpr) => {
                this.camera.left = -width / 2;
                this.camera.right = width / 2;
                this.camera.top = height / 2;
                this.camera.bottom = -height / 2;

                if (width < breakpoint) {
                    this.camera.zoom = 0.8;
                } else {
                    this.camera.zoom = 1;
                }

                this.camera.updateProjectionMatrix();

                width = Math.round(width * dpr);
                height = Math.round(height * dpr);

                this.resolution.value.set(width, height);
                this.texelSize.value.set(1 / width, 1 / height);
                this.aspect.value = width / height;
            };

            static update = (time, delta, frame) => {
                this.time.value = time;
                this.frame.value = frame;
            };
        }

        class App {
            static async init() {
                this.initWorld();
                this.initViews();
                this.initControllers();

                this.addListeners();
                this.onResize();

                this.initPanel();
            }

            static initWorld() {
                WorldController.init();
                document.body.appendChild(WorldController.element);
            }

            static initViews() {
                this.view = new SceneView();
                WorldController.scene.add(this.view);

                this.ui = new UI({ fps: true });
                this.ui.animateIn();
                document.body.appendChild(this.ui.element);
            }

            static initControllers() {
                const { renderer, scene, camera } = WorldController;

                RenderManager.init(renderer, scene, camera);
            }

            static initPanel() {
                PanelController.init(this.ui);
            }

            static addListeners() {
                window.addEventListener('resize', this.onResize);
                ticker.add(this.onUpdate);
                ticker.start();
            }

            // Event handlers

            static onResize = () => {
                const width = document.documentElement.clientWidth;
                const height = document.documentElement.clientHeight;
                const dpr = window.devicePixelRatio;

                WorldController.resize(width, height, dpr);
                RenderManager.resize(width, height, dpr);
            };

            static onUpdate = (time, delta, frame) => {
                WorldController.update(time, delta, frame);
                RenderManager.update(time, delta, frame);
                this.ui.update();
            };
        }

        App.init();
    </script>
</head>
<body>
</body>
</html>
